package com.demo.money.controller.app;

import com.demo.money.kit.JSONResult;
import com.demo.money.kit.example.NewsExample;
import com.demo.money.kit.example.UserExample;
import com.demo.money.model.News;
import com.demo.money.model.Type;
import com.demo.money.service.AppAllService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: zhangfengpu
 * @date: 2020/4/2
 */
@RestController
@RequestMapping("news")
public class AppAllNewsController {
    @Autowired
    private AppAllService appAllService;
    @GetMapping("/list")
    public JSONResult list(String typeId){
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        criteria.andTypeIdEqualTo(typeId);
        List<News> list = appAllService.selectNewsByExample(newsExample);
        return JSONResult.ok(list);
    }

    @GetMapping("/byid/{id}")
    public JSONResult byid(@PathVariable String id){
        NewsExample newsExample = new NewsExample();
        NewsExample.Criteria criteria = newsExample.createCriteria();
        criteria.andIdEqualTo(id);

        List<News> list = appAllService.selectNewsByExample(newsExample);

        News news;
        try {
            news = list.get(0);
        }catch (Exception e){
            news = null;
        }
        return JSONResult.ok(news);
    }
}
